package router

import (
	"aiserver/pkg/config"
	"aiserver/pkg/controllers"
	"aiserver/pkg/store/mysql"

	"gitee.com/wujianqiang/golibs/pkg/logging"
	"github.com/goccy/go-json"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cors"
	"github.com/gofiber/fiber/v2/middleware/requestid"
)

var log = logging.GetLogger()

func Router() *fiber.App {
	// call recovery if panic happen

	defer func() {
		if r := recover(); r != nil {
			log.Error(r)
		}
	}()
	cnf := fiber.Config{
		JSONEncoder:       json.Marshal,
		JSONDecoder:       json.Unmarshal,
		AppName:           "aiserver: 1.0",
		StreamRequestBody: true,
	}
	app := fiber.New(cnf)
	app.Use(cors.New())
	app.Use(requestid.New())
	storeds := mysql.GetMysqlFactory(config.Appconfig.DB, config.Appconfig.RDS, config.Appconfig.SPDB)
	user := controllers.NewUserController(storeds)
	api := app.Group("/api")
	api.Get("/sso", user.SSo)
	api.Post("/reflesh", user.RefleshToken)

	return app
}
